﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BLL;
namespace Web.admin.InnerManage
{
    public partial class AddInnerMon : System.Web.UI.Page
    {
        
        private MoneyService moneyService = new MoneyService();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BoContests contest = new BoContests(); 
                string operation = Request["OPERATION"];
                if (operation == null)
                {
                    string contestId = Request["ContestId"];
                    
                    ContestId.Value = contestId;
                    contest.CON_ID = contestId;
                    Repeater1.DataSource = moneyService.GetContestMoney(contest, BoMoney.MONEYTYPE_INNER);
                    Repeater1.DataBind();
                    return;
                }

                string moneyId = Request["id"];
                if (moneyId == null)
                    return;

                BoMoney money = moneyService.GetMoney(moneyId);
                if (money == null)
                    return;

                contest.CON_ID = money.MON_CONTESTS;
                Repeater1.DataSource = moneyService.GetContestMoney(contest, BoMoney.MONEYTYPE_INNER);
                Repeater1.DataBind();

                if (operation == "Modify")
                {
                    ModifyMoney(moneyId);
                }
                else if (operation == "Delete")
                {
                    DeleteMoney(moneyId);
                }
            }
        }
        protected void ModifyMoney(string moneyId)
        {
            MoneyId.Value = moneyId;

            BoMoney money = moneyService.GetMoney(moneyId);

            txtItem.Text = money.MON_ITEM;
            txtComment.Text = money.MON_COMMENT;
            txtMoney.Text = money.MON_NUMBER.ToString();
            txtDealer.Text = money.MON_DEALER;
            txtDate.Text = money.MON_DATE.ToString();
        }

        protected void DeleteMoney(string teacherId)
        {

        }

        protected void btn_Save_Click(object sender, EventArgs e)
        {
            BoMoney money = null;
            if (MoneyId.Value == "")
            {
                money = new BoMoney();
                money.MON_CONTESTS = ContestId.Value;
            }
            else
                money = moneyService.GetMoney(MoneyId.Value);

            money.MON_ITEM = txtItem.Text;
            money.MON_COMMENT = txtComment.Text;
            money.MON_NUMBER = Double.Parse(txtMoney.Text);
            money.MON_TYPE = 1;
            money.MON_DEALER = txtDealer.Text;
            money.MON_DATE = DateTime.Parse(txtDate.Text);

            if (MoneyId.Value == "")
                moneyService.CreateSubmit(money);
            else
                moneyService.UpdateMoney(money);

            Response.Redirect("InnerMoney.aspx?id=" + money.MON_CONTESTS);
        }
    }
}